package frames;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import constants.*;

public class MainPanel extends JPanel {
	// component
	private JButton button;
	private MainFrame frame;

	// event handler
	private EventHandler eventHandler;

	// variable
	Image backGroundImg;

	public MainPanel() {
		setBackground(Constants.PANEL_BACKGROUND_COLOR);

		eventHandler = new EventHandler();

		backGroundImg = Constants.MAIN_IMG;

		for (int i = 0; i < Constants.MAIN_BUTTON.values().length; i++) {
			button = new JButton();
			button.setIcon(new ImageIcon("./img/icon/" + Constants.MAIN_BUTTON.values()[i].toString() + ".png"));
			button.setActionCommand(Constants.MAIN_BUTTON.values()[i].toString());
			button.addActionListener(eventHandler);
			button.setBounds(80 + i * 70, 150, 50, 50);
			this.add(button);
		}

	}

	public void init(MainFrame frame) {
		this.frame = frame;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		// g.drawImage(backGroundImg, 0, 0, this);
		g.drawImage(backGroundImg, 20, 30, this);

	}

	private class EventHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand() == Constants.MAIN_BUTTON.NEW.toString()) {
				frame.setInputPanel();
			} else if (e.getActionCommand() == Constants.MAIN_BUTTON.OPEN.toString()) {
				frame.setOpenPanel();
			}
		}
	}

}
